# core/CMakeLists.txt

# 设置核心库的版本信息
set(NANOK_CORE_LIB_VERSION_MAJOR 1)
set(NANOK_CORE_LIB_VERSION_MINOR 0)
set(NANOK_CORE_LIB_VERSION_PATCH 0)
set(NANOK_CORE_LIB_VERSION ${NANOK_CORE_LIB_VERSION_MAJOR}.${NANOK_CORE_LIB_VERSION_MINOR}.${NANOK_CORE_LIB_VERSION_PATCH})

# 收集所有源文件
file(GLOB_RECURSE NANOK_CORE_SOURCES
    "*.cpp"
)

# 收集所有头文件（用于IDE显示）
file(GLOB_RECURSE NANOK_CORE_HEADERS
    "*.h"
    "*.hpp"
)

# 创建核心库
add_library(nanok_core_lib
    ${NANOK_CORE_SOURCES}
    ${NANOK_CORE_HEADERS}
)

# 设置库的属性
set_target_properties(nanok_core_lib PROPERTIES
    VERSION ${NANOK_CORE_LIB_VERSION}
    SOVERSION ${NANOK_CORE_LIB_VERSION_MAJOR}
    OUTPUT_NAME "nanok_core"
    CXX_STANDARD 20
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
)

# 设置包含目录
# 使用PUBLIC以便依赖此库的目标自动包含这些目录
target_include_directories(nanok_core_lib
    PUBLIC 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../include>  # 如果头文件在上级include目录
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../tpps/spdlog/include>  # 如果头文件在上级include目录
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../tpps/googletest/googletest/include>  # 如果头文件在上级include目录
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}  # 用于实现文件
)

# 添加编译选项
target_compile_options(nanok_core_lib
    PRIVATE
        -Wall
        -Wextra
        -Wpedantic
        -Werror  # 可根据需要开启或关闭
)

# 添加依赖项（如果有）
# find_package(SomeDependency REQUIRED)
target_link_libraries(nanok_core_lib PRIVATE 
    ${MHD_LIBRARIES}
    ${JSONCPP_LIBRARIES}
    ${CURL_LIBRARIES}
    ${LEVELDB_LIBRARIES}
    pthread
    spdlog::spdlog
)

# 如果core模块依赖项目中的其他库
# target_link_libraries(core_lib PRIVATE another_lib)





# 安装规则（可选）
install(TARGETS nanok_core_lib
    EXPORT CoreLibraryTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# 安装头文件（可选）
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/nanok_core
    FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)

# 导出目标（用于其他CMake项目使用此库）
install(EXPORT CoreLibraryTargets
    FILE CoreLibraryTargets.cmake
    NAMESPACE NanokCore::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/NanokCore
)

message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}; CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}; ???????")
# 生成并安装配置文件（可选，用于find_package）
include(CMakePackageConfigHelpers)
configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/NanokCoreConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/NanokCoreConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/NanokCore
)

write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/NanokCoreConfigVersion.cmake
    VERSION ${NANOK_CORE_LIB_VERSION}
    COMPATIBILITY SameMajorVersion
)

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/NanokCoreConfig.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/NanokCoreConfigVersion.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/NanokCore
)







# 添加子目录（如果core模块有子模块）
# add_subdirectory(submodule)

# 单元测试（如果在此目录下有测试）
if(BUILD_TESTING)
    enable_testing()
    
    # 创建测试可执行文件
    add_executable(nanok_core_tests
        test/test_main.cpp
        # 添加其他测试文件
    )
    
    # 链接测试框架和核心库
    target_link_libraries(nanok_core_tests
        PRIVATE
            nanok_core_lib
            GTest::gtest_main
    )
    
    # 添加测试
    add_test(NAME NanokCoreTests COMMAND nanok_core_tests)
    
    # 设置测试属性
    set_tests_properties(NanokCoreTests PROPERTIES
        TIMEOUT 30
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    )
endif()